<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:include page="/WEB-INF/jsps/include/amazeUI.jsp" />
<!doctype html>
<div class="am-u-sm-12 am-u-md-10 am-u-lg-8 am-u-sm-centered">
	<form id="editForm" class="am-form">
		<div class="am-form-group">
			<label for="rolename">角色名称</label> <input type="text" value="${role.rolename }" name="rolename" id="rolename" placeholder="角色名称">
		</div>
	
		<div class="am-form-group">
			<label for="treeDemo">角色权限</label>
			<ul id="treeDemo" class="ztree"></ul>
		</div>
		<input type="hidden" value="${role.id }" name="id">
		<input type="hidden" value="${role.premissionsStr }" name="premissionsStr" id="premissionsStr">
		<button type="button" class="am-btn am-btn-success" id="confirmBtnID" >确定</button>
		<button type="button" class="am-btn am-btn-danger" id="cancelBtnID">取消</button>
	</form>
</div>

<script type="text/javascript">
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
$(function(){
	ztreeInit();		//初始化ztree
	cancelBtnClick();	//取消按钮点击事件
	confirmBtnClick();	//确定按钮点击事件
});

//初始化ztree
function ztreeInit(){
	var setting = {
		async: { enable: false},
		check: {
			enable: true,
			chkStyle:"checkbox",
			chkboxType: { "Y": "s", "N": "s" }
		},
		callback: {
			onCheck: zTreeOnCheck
		},
		data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",
				rootPId: 0,
			}
		}
	};
	var zNodes=null;
	var roleId = ${role.id};
	$.ajax({
		type:"post",
		url:"<%=request.getContextPath()%>/permissions/permissionTreeOfRole",
		async : false, 
		data:{rid:roleId},
		success:function(data){
			zNodes = data;
			//prossessData(zNodes);
		}
	});
	var zTreeObj = $.fn.zTree.init($("#treeDemo"),setting, zNodes);
	zTreeObj.expandAll(true);
}
/**
 * 树的节点被勾选
 */
function zTreeOnCheck(event, treeId, treeNode){
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	var nodes = treeObj.getCheckedNodes(true);
	var checkedIdStr = "";
	for(var index in nodes){
		checkedIdStr = checkedIdStr + nodes[index].id +",";
	}
	checkedIdStr = checkedIdStr.substring(0,checkedIdStr.length-1);
	$("#premissionsStr").val(checkedIdStr);
	
}
/* //处理节点数据
function prossessData(zNodes){
	
} */
//点击取消按钮
function cancelBtnClick(){
	$("#cancelBtnID").click(function(){
		console.log("关窗");
		parent.layer.close(index);
	});
}
//确定按钮点击
function confirmBtnClick(){
	 $("#confirmBtnID").click(function(){
		console.log("确定");
		//do something...
		$.ajax({
			type:"post",
			url:"editRoleSubmit",
			data:$("#editForm").serialize(),
			success:function(data){
				console.log(data);
				//parent.location.reload();
				parent.$("#rightContent").load("role/roleList");
				parent.layer.close(index);
			}
		});
	});
}

</script>
